package com.lemon.smartsafetynew.ui.adapter.patrol;

import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemVo;
import com.lemon.smartsafetynew.util.patrol.CheckItemUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/8/1.
 */

public class PatrolTaskItemAdapter extends LemonRcvAdapter<SafePatrolTaskItemVo> {

    private RecyclerTClickListener<SafePatrolTaskItemVo> mListener;

    public PatrolTaskItemAdapter(@Nullable List<SafePatrolTaskItemVo> data,
                                 RecyclerTClickListener<SafePatrolTaskItemVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new SafePatrolItem();
    }

    private class SafePatrolItem implements AdapterItem<SafePatrolTaskItemVo> {

        private View whiteTopLineView;
        private View whiteBottomLineView;
        private LinearLayout safePatrolItemLayout;

        private ImageView safePatrolItemStatusImageView;
        private ImageView qrImageView;
        private ImageView nfcImageView;
        private ImageView safePatrolPointImageView;

        private TextView safePatrolItemNameTv;
        private TextView safePatrolItemDescTv;

        private SafePatrolTaskItemVo mData;
        private int mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_patrol_task_item;
        }

        @Override
        public void bindViews(View root) {
            safePatrolItemStatusImageView = (ImageView) root.findViewById(R.id.safe_patrol_item_status_ImageView);
            safePatrolItemNameTv = (TextView) root.findViewById(R.id.safe_patrol_item_name_tv);
            safePatrolItemDescTv = (TextView) root.findViewById(R.id.safe_patrol_item_desc_tv);
            safePatrolItemLayout = (LinearLayout) root.findViewById(R.id.safe_patrol_item_layout);

            qrImageView = (ImageView) root.findViewById(R.id.qr_ImageView);
            nfcImageView = (ImageView) root.findViewById(R.id.nfc_ImageView);
            safePatrolPointImageView = (ImageView) root.findViewById(R.id.safe_patrol_point_imageView);

            whiteTopLineView = root.findViewById(R.id.white_top_line_view);
            whiteBottomLineView = root.findViewById(R.id.white_bottom_line_view);
        }

        @Override
        public void setViews() {

            safePatrolItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(SafePatrolTaskItemVo patrolTaskItemVo, int position) {

            mData = patrolTaskItemVo;
            mPosition = position;

            if (position == 0) {
                whiteTopLineView.setVisibility(View.VISIBLE);
                whiteBottomLineView.setVisibility(View.GONE);
            } else {
                whiteTopLineView.setVisibility(View.GONE);
                whiteBottomLineView.setVisibility(View.GONE);
            }

            safePatrolItemNameTv.setText(patrolTaskItemVo.getObject_title());
            safePatrolItemDescTv.setText(patrolTaskItemVo.getBuilding_name()
                    + " - "
                    + patrolTaskItemVo.getFloor_number() + "楼 "
                    + patrolTaskItemVo.getPosition_name());

            safePatrolItemStatusImageView.setImageResource(CheckItemUtil.getCheckItemStatusImgRes(patrolTaskItemVo.getStatus()));

            if(patrolTaskItemVo.getIs_patrol() == 1) { // 1： 巡更点
                safePatrolPointImageView.setVisibility(View.VISIBLE);
            } else {
                safePatrolPointImageView.setVisibility(View.INVISIBLE);
            }

            if(StringUtils.isEmpty(patrolTaskItemVo.getQrcode_code())) {
                qrImageView.setVisibility(View.GONE);
            } else {
                qrImageView.setVisibility(View.VISIBLE);
            }

            if(StringUtils.isEmpty(patrolTaskItemVo.getNfc_code())) {
                nfcImageView.setVisibility(View.GONE);
            } else {
                nfcImageView.setVisibility(View.VISIBLE);
            }

        }
    }
}

